﻿using System;
using ChessKit.Common;

namespace ChessKit.Ui
{
  public static class BoardTransform
  {
    public static readonly IBoardLocationTransform Identity = new IdentityTransform();
    public static readonly IBoardLocationTransform Rotated = new RotatedTransform();

    private class IdentityTransform : IBoardLocationTransform
    {
      public BoardLocation Tranform(BoardLocation location)
      {
//        return location;
        /*
         * We need this transformation because BoardPanel
         * has usual for UI axis direction, which doesn't
         * match "chess axis" direction niether for straight
         * nor for reversed view
         */
        switch (location.Area)
        {
          case BoardArea.Background:
          case BoardArea.Unknown:
          case BoardArea.Column:
          case BoardArea.Row:
          case BoardArea.ShortColumn:
          case BoardArea.ShortRow:
            return location;

          case BoardArea.LeftEdgeCell:
            return BoardLocation.RightEdge(location.Y);
          case BoardArea.RightEdgeCell:
            return BoardLocation.LeftEdge(location.Y);
          case BoardArea.TopEdgeCell:
            return BoardLocation.TopEdge(9 - location.X);
          case BoardArea.BottomEdgeCell:
            return BoardLocation.BottomEdge(9 - location.X);

          case BoardArea.TopLeftCorner:
            return BoardLocation.TopRightCorner;
          case BoardArea.TopRightCorner:
            return BoardLocation.TopLeftCorner;
          case BoardArea.BottomRightCorner:
            return BoardLocation.BottomLeftCorner;
          case BoardArea.BottomLeftCorner:
            return BoardLocation.BottomRightCorner;

          case BoardArea.Cell:
            return BoardLocation.Cell(9 - location.X, location.Y);
        }
        throw new NotSupportedException(location.Area.ToString());
      }
    }
    private class RotatedTransform : IBoardLocationTransform
    {
      public BoardLocation Tranform(BoardLocation location)
      {
//        return location;
        switch (location.Area)
        {
          case BoardArea.Background:
          case BoardArea.Unknown:
          case BoardArea.Column:
          case BoardArea.Row:
          case BoardArea.ShortColumn:
          case BoardArea.ShortRow:
            return location;

          case BoardArea.LeftEdgeCell:
            return BoardLocation.LeftEdge(9 - location.Y);
          case BoardArea.RightEdgeCell:
            return BoardLocation.RightEdge(9 - location.Y);
          case BoardArea.TopEdgeCell:
            return BoardLocation.BottomEdge(location.X);
          case BoardArea.BottomEdgeCell:
            return BoardLocation.TopEdge(location.X);

          case BoardArea.TopLeftCorner:
            return BoardLocation.BottomLeftCorner;
          case BoardArea.TopRightCorner:
            return BoardLocation.BottomRightCorner;
          case BoardArea.BottomRightCorner:
            return BoardLocation.TopRightCorner;
          case BoardArea.BottomLeftCorner:
            return BoardLocation.TopLeftCorner;

          case BoardArea.Cell:
            return BoardLocation.Cell(location.X, 9 - location.Y);
        }
        throw new NotSupportedException(location.Area.ToString());
      }
    }
  }
}